package com.dwd.hrm.service.impl;

import com.dwd.hrm.domain.SendSmsDto;
import com.dwd.hrm.domain.SendSmsVo;
import com.dwd.hrm.domain.VipUser;
import com.dwd.hrm.exception.GlobalException;
import com.dwd.hrm.mapper.VipUserMapper;
import com.dwd.hrm.service.IVerifycodeService;
import com.dwd.hrm.util.StrUtils;
import com.dwd.hrm.util.VerifyCodeUtils;
import com.dwd.hrm.utils.ValidUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @Description: TODO
 * @Author 汶狄
 * @Date 2021/3/11 14:26
 * @Version 1.0
 */
@Service
@Slf4j
public class VerifycodeServiceImpl implements IVerifycodeService {
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private VipUserMapper vipUserMapper;

    /**
     * 获取验证码
     *
     * @param key
     * @return
     */
    @Override
    public String imageCode(String key) throws IOException {
        //判断
        ValidUtils.assertNotNull(key, "刷新 重试");
        //生成验证码
        String code = UUID.randomUUID().toString().substring(0, 4);
        //存入redis
        redisTemplate.opsForValue().set(key, code, 10, TimeUnit.MINUTES);
        //合成图片，转64编码
        return VerifyCodeUtils.VerifyCode(140, 40, code);
    }

    /**
     * 发送手机验证码
     *
     * @param sendSmsDto
     */
    @Override
    public void sendSmsCode(SendSmsDto sendSmsDto) {
        String imageCode = sendSmsDto.getImageCode();
        //前台key
        String imageCodeKey = sendSmsDto.getImageCodeKey();
        String mobile = sendSmsDto.getMobile();
        //校验数据
        ValidUtils.assertNotNull(mobile, "手机为空");
        VipUser vipUser1 = vipUserMapper.queryByPhone(mobile);
        //手机号校验
        if (vipUser1 != null) {
            throw new GlobalException("手机重复");
        }
        ValidUtils.assertNotNull(imageCode, "图片验证码不能为空");
        ValidUtils.assertNotNull(imageCodeKey, "系统错误，请刷新重试");

        //从数据库取出图片验证码判断
        String imgCodeRedis = (String) redisTemplate.opsForValue().get(imageCodeKey);
        //判断
        String SmsKey = "REG" + mobile;
        Date date = new Date();
        //失败返回
        ValidUtils.assertNotEqual(imgCodeRedis, imageCode.toLowerCase(), "图片验证码错误");
        //成功
        //判断redis是否存在验证码
        SendSmsVo sendSmsVo = (SendSmsVo) redisTemplate.opsForValue().get(SmsKey);
        String smsCode;
        if (sendSmsVo != null) {
            //判断时间是否再60s内
            if ((date.getTime() - sendSmsVo.getTime()) / 1000 < 60) {
                //是  刷新频繁
                throw new GlobalException("刷新频繁");
            }
            //否  获取之前的验证码 设置
            smsCode = sendSmsVo.getCode();
        } else {
            //否
            //生成验证码 reg
            smsCode = StrUtils.getRandomString(4);
        }
        //存入redis key为手机号和标识  value是验证码和时间  10分钟过期
        redisTemplate.opsForValue().set(SmsKey, new SendSmsVo(smsCode, date.getTime()), 10, TimeUnit.MINUTES);
        //调用接口发送短信
        log.info(smsCode);
        //存入数据库
        log.info(smsCode);
    }
}
